<ui:composition template="/WEB-INF/templates/template.xhtml"
                xmlns="http://www.w3.org/1999/xhtml"
                xmlns:h="http://java.sun.com/jsf/html"
                xmlns:f="http://java.sun.com/jsf/core"
                xmlns:ui="http://java.sun.com/jsf/facelets"
                xmlns:p="http://primefaces.org/ui">

    <ui:define name="title">#{txt['home.title']}</ui:define>

    <ui:define name="head">
        <h:form>
            <p:toolbar>
                <p:toolbarGroup align="left">
                    <p:commandButton value="#{txt['home.button.newForum']}"
                                     oncomplete="newForumDlg.show()"/>
                    <p:commandButton value="#{txt['home.button.subscribe']}"
                                     oncomplete="subscriptionsDlg.show()" />
                </p:toolbarGroup>
                <p:toolbarGroup align="right">
                    <p:commandButton value="#{txt['home.button.logout']} (#{homeBean.actMember.nickname})"
                                     action="#{logoutBean.logout()}"/>
                </p:toolbarGroup>
            </p:toolbar>

        </h:form>

    </ui:define>

    <p:messages id="msgs"/>

    <ui:define name="left">
        <h:form id="myForenForm">
            <p:dataTable id="myForen" var="foren" value="#{homeBean.myForen}"
                         rowKey="#{foren.OID}" selection="#{homeBean.actForum}"
                         selectionMode="single" >
                <p:ajax event="rowSelect" update=":myEntriesForm:entriesID :myForenForm"/>
                <p:column headerText="#{txt['home.table.column.title']}">
                    #{foren.title}
                </p:column>
                <p:column headerText="#{txt['subscriptions.table.column.member']}">
                    #{foren.memberCount}
                </p:column>
                <p:column headerText="#{txt['subscriptions.table.column.entries']}">
                    #{foren.entryCount}
                </p:column>
            </p:dataTable>
        </h:form>
    </ui:define>

    <ui:define name="content">
        <f:view locale="#{language.currentLocale}">
            <h:form id="myEntriesForm">
                <p:dataTable id="entriesID" var="entries" value="#{homeBean.actEntries}" rowKey="#{entries.OID}"
                             paginator="true" paginatorPosition="bottom"
                             paginatorAlwaysVisible="false" rows="20" selection="#{homeBean.actEntry}"
                             selectionMode="single" emptyMessage="#{txt['global.table.empty']}">
                    <f:facet name="header">
                        <h:outputText value="#{txt['home.talbe.entries']} (#{homeBean.actForum.title})" />
                    </f:facet>
                    <p:ajax event="rowSelect" update=":entry" oncomplete="entryDlg.show()"/>
                    <p:column headerText="#{txt['home.table.column.title']}">
                        #{entries.title}
                    </p:column>
                    <p:column headerText="#{txt['home.table.column.author']}">
                        #{entries.writtenBy.nickname}
                    </p:column>
                    <p:column headerText="#{txt['home.table.column.text']}">
                        #{entries.text}
                    </p:column>
                    <f:facet name="footer">
                        <p:commandButton value="#{txt['home.button.newEntry']}"
                                         oncomplete="newEntryDlg.show()" disabled="#{homeBean.checkAddEntry}"/>
                    </f:facet>
                </p:dataTable>
            </h:form>
        </f:view>
    </ui:define>

    <ui:define name="right">
        <h:form id="searchForm" > <!-- searchDlg -->
                <p:panel id="search_fields">
                    <p:panelGrid columns="3">
                        <h:outputLabel for="search_txtFld_searchForTitle" value="#{txt['search.label.searchForTitle']}" />
                        <h:inputText id="search_txtFld_searchForTitle" value="#{search.searchTerm}" />
                        <p:commandButton id="search_btn_searchForTitle" icon="ui-icon-search"
                                         actionListener="#{search.findEntriesByTitle}" update=":searchForm:search_fields :myEntriesForm:entriesID :myForenForm :growlForm"
                                         process="@this,search_txtFld_searchForTitle"/>

                        <h:outputLabel for="search_txtFld_searchForAuthor" value="#{txt['search.label.searchForAuthor']}" />
                        <h:inputText id="search_txtFld_searchForAuthor" value="#{search.searchTerm}" />
                        <p:commandButton id="search_btn_searchForAuthor" icon="ui-icon-search"
                                         actionListener="#{search.findEntriesByAuthor}" update=":searchForm:search_fields :myEntriesForm:entriesID :myForenForm :growlForm"
                                         process="@this,search_txtFld_searchForAuthor"/>
                    </p:panelGrid>
                </p:panel>
        </h:form> <!-- /searchDlg -->
    </ui:define>

    <ui:define name="appendBody">
        <h:form id="newForumForm"> <!-- newForumDlg -->
            <p:dialog widgetVar="newForumDlg" resizable="false" modal="true" hideEffect="fade">
                <f:facet name="header">
                    <p:outputLabel value="#{txt['newForum.title']}" />
                </f:facet>
                <f:facet name="footer">
                    <p:commandButton id="addButton" process="@form" icon="ui-icon-plus"
                                     actionListener="#{homeBean.addForum}"
                                     update=":myForenForm:myForen :newForumForm :growlForm"
                                     value="#{txt['newForum.button.add']}"/>
                </f:facet>
                <p:panelGrid>
                    <p:row>
                        <p:column>
                            <h:outputLabel for="name" value="#{txt['newForum.label.name']}" />
                        </p:column>
                        <p:column>
                            <p:inputText id="name" value="#{homeBean.title}"/>
                        </p:column>
                    </p:row>
                </p:panelGrid>
            </p:dialog>
        </h:form> <!-- /newForumDlg -->

        <h:form id="newEntryForm"> <!-- newEntryDlg -->
            <p:dialog widgetVar="newEntryDlg" resizable="false" modal="true" hideEffect="fade">
                <f:facet name="header">
                    <p:outputLabel value="#{txt['newEntry.title']}" />
                </f:facet>
                <f:facet name="footer">
                    <p:commandButton id="addButton" process="@form" icon="ui-icon-plus"
                                     actionListener="#{homeBean.addEntry}"
                                     update=":myEntriesForm:entriesID :myForenForm:myForen :newEntryForm :growlForm"
                                     value="#{txt['newEntry.button.add']}"/>
                </f:facet>
                <p:panelGrid>
                    <p:row>
                        <p:column>
                            <h:outputLabel for="title" value="#{txt['newEntry.label.title']}" />
                        </p:column>
                        <p:column>
                            <p:inputText id="title" value="#{homeBean.title}"/>
                        </p:column>
                    </p:row>
                    <p:row>
                        <p:column>
                            <h:outputLabel for="text" value="#{txt['newEntry.label.text']}" />
                        </p:column>
                        <p:column>
                            <p:inputTextarea id ="text" value="#{homeBean.text}"/>
                        </p:column>
                    </p:row>
                </p:panelGrid>
            </p:dialog>
        </h:form> <!-- /newEntryDlg -->

        <h:form id="allForenForm" > <!-- subscriptionsDlg -->
            <p:dialog id="allForenDlgID" widgetVar="subscriptionsDlg"
                      resizable="false" modal="true" hideEffect="fade">
                <f:facet name="header">
                    <p:outputLabel value="#{txt['subscriptions.title']}" />
                </f:facet>
                <p:dataTable id="allForen" var="forum" value="#{homeBean.allForen}"
                             rowKey="#{forum.OID}" selection="#{homeBean.actForum}"
                             selectionMode="single">
                    <p:column headerText="#{txt['subscriptions.table.column.title']}">
                        <h:outputText value="#{forum.title}" />
                    </p:column>
                    <p:column headerText="#{txt['subscriptions.table.column.member']}">
                        <h:outputText value="#{forum.memberCount}" />
                    </p:column>
                    <p:column headerText="#{txt['subscriptions.table.column.entries']}">
                        <h:outputText value="#{forum.entryCount}" />
                    </p:column>
                </p:dataTable>
                <f:facet name="footer">
                    <p:commandButton id="addButton" value="#{txt['subscriptions.button.subscribe']}"
                                     process="@form" icon="ui-icon-plus" actionListener="#{homeBean.addSubscriber}"
                                     update=":myForenForm:myForen :allForenForm"/>
                </f:facet>
            </p:dialog>
        </h:form> <!-- /subscriptionsDlg -->

        <h:form id="entry"> <!-- newEntryDlg -->
            <p:dialog id="entryDlgID" widgetVar="entryDlg" resizable="false" modal="false" hideEffect="fade">
                <f:facet name="header">
                    <p:outputLabel value="#{homeBean.actEntry.title}" />
                </f:facet>
                <p:panelGrid id="entryPanel">
                    <p:row>
                        <p:column>
                            <h:outputLabel for="title" value="#{txt['home.table.column.title']}" />
                        </p:column>
                        <p:column>
                            <h:outputText value="#{homeBean.actEntry.title}"/>
                        </p:column>
                    </p:row>
                    <p:row>
                        <p:column>
                            <h:outputLabel for="text" value="#{txt['home.table.column.author']}" />
                        </p:column>
                        <p:column>
                            <h:outputText value="#{homeBean.actEntry.writtenBy}"/>
                        </p:column>
                    </p:row>
                    <p:row>
                        <p:column>
                            <h:outputLabel for="text" value="#{txt['home.table.column.text']}" />
                        </p:column>
                        <p:column>
                            <h:outputText value="#{homeBean.actEntry.text}"/>
                        </p:column>
                    </p:row>
                </p:panelGrid>
            </p:dialog>
        </h:form> <!-- /entryDlg -->
    </ui:define>
</ui:composition>
